package com.group.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group.dao.GeneralBeanDao;
import com.group.model.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.base.view.BaseResult;
import com.base.view.GeneralResult;
import com.base.view.ListResult;
import com.group.interface1.RequireSuperAdmin;
import com.group.interface1.ValidPagePara;
import com.group.util.NoteException;

/**
 * 后台用户管理
 * 
 * @author zhaolei
 *
 */
@Controller
public class BackUserController extends BaseController {

	/**
	 * 后台用户登录
	 * @param user 用户id
	 * @param response 返回
	 * @return 返回
	 */
	@PostMapping(value = { "/back/user/login", "/back/admin/login" })
	@ResponseBody
	public BaseResult login(@RequestBody BackUser user, HttpServletResponse response) {
		if (StringUtils.isEmpty(user.getName()) || StringUtils.isEmpty(user.getPassword())) {
			throw new NoteException("账号密码不能为空");
		}

		BackUser exist = beanDao.queryOne(user);
		if (exist == null) {
			throw new NoteException("账号密码错误");
		}

		addUserCookie(response, "backUserId", exist.getUuid());

		return new BaseResult();
	}

	/**
	 * 查询当前登录的管理员
	 * 
	 * @param request request
	 * @return User
	 */
	@GetMapping(value = { "/back/user/whoami", "/back/admin/whoami" })
	@ResponseBody
	public GeneralResult<BackUser> whoami(HttpServletRequest request) {

		return new GeneralResult<BackUser>().setValue(getBackUser(request));
	}

	/**
	 * 登出
	 * 
	 * @param response response
	 * @return baseresult
	 */
	@GetMapping(value = { "/back/user/logout", "/back/admin/logout" })
	@ResponseBody
	public BaseResult logout(HttpServletResponse response) {
		addUserCookie(response, "backUserId", null);
		return new BaseResult();
	}

	/**
	 * 后台查询用户
	 * @param key key
	 * @param type 用户类型
	 * @param start 开始
	 * @param limit 每页的条数
	 * @param date 时间
	 * @return 用户列表
	 */
	@GetMapping("/back/user/query")
	@ResponseBody
	@ValidPagePara
	public ListResult<User> queryUser(String key, String type, Integer start, Integer limit,String date) {
		if (limit > 100) {
			limit = 100;
		}

		return new ListResult<User>().setList(userDao.queryUser(key, type, start, limit,date)).setTotal(userDao.queryUserCount(key, type,date));
	}

	/**
	 * 更新用户
	 * 
	 * @param user 用户实体
	 * @return 返回
	 */
	@PutMapping("/back/user/update")
	@ResponseBody
	public BaseResult updateUser(@RequestBody User user) {

		beanDao.save(user);

		return new BaseResult();
	}

	/**
	 * 修改用户钱包币的数量
	 * @param userId 用户id
	 * @param num 增减数额
	 * @param cid cid
	 * @return BaseResult
	 */
	@GetMapping("/back/user/ticker/update")
	@ResponseBody
	@RequireSuperAdmin
	public BaseResult updateUserTicker(Long userId, String cid, Double num) {
		// UserTicker ut = tickerService.getUserTicker(userId, cid);
		tickerService.addTicker(userId, cid, num, "客服修改");

		return new BaseResult();
	}

	/**
	 * 添加用户
	 * 
	 * @param user 用户
	 * @return User
	 */
	@PostMapping("/back/user/add")
	@ResponseBody
	public GeneralResult<User> addUser(@RequestBody User user) {
		user.setId(null);
		user.setUuid(UUID.randomUUID().toString());
		beanDao.save(user);

		return new GeneralResult<User>().setValue(user);
	}

	/**
	 * 获取系统参数
	 * @param start 开始
	 * @param limit 条数
	 * @return List
	 */
	@GetMapping("/back/system/param")
	@ResponseBody
	public ListResult getSystemParam(Integer start, Integer limit){
		return new ListResult().setList(bbsDao.querySystemParamList((start-1)*limit, limit)).setTotal(bbsDao.queryCount());
	}

	/**
	 * 修改系统参数
	 * @param sp SystemPara
	 * @return GeneralResult
	 */
	@PostMapping("/back/system/updateParam")
	@ResponseBody
	public GeneralResult updateById(@RequestBody SystemPara sp){
		beanDao.save(sp);
		return new GeneralResult();
	}

	/**
	 * 冻结用户
	 * @param userId 用户id
	 * @param status 状态
	 * @return user
	 */
	@GetMapping("/back/user/freezeUser")
	@ResponseBody
	public GeneralResult freezeUser(
			@RequestParam Long userId,
			@RequestParam Integer status){
		User user = beanDao.quickQueryOne(User.class, "id", userId);
		user.setFreezeStatus(status);
		beanDao.save(user);
		return new GeneralResult().setValue(user);
	}

	@GetMapping("/back/user/freezeList")
	@ResponseBody
	@ValidPagePara
	public ListResult<User> getFreezeList(Integer start, Integer limit,Integer freezeStatus){
		if (limit > 100) {
			limit = 100;
		}
		return new ListResult<User>().setList(userDao.queryFreezeUser(freezeStatus,start, limit)).setTotal(userDao.queryFreezeCount(freezeStatus));
	}



}
